package imserver.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.Session;

import imserver.beans.MessageInfo;
import imserver.common.util.DaoException;
import imserver.constant.MessageStatus;
import imserver.dao.BaseDaoImpl;
import imserver.service.MemberService;
import imserver.service.MessageService;

public class MessageServiceImpl extends BaseDaoImpl<MessageInfo> implements MessageService{
	
	@Resource
	private MemberService memberService;
	
	@Override
	public void send(String sender, String to, String content) throws DaoException {
		MessageInfo messageInfo = new MessageInfo();
		messageInfo.setContent(content);
		messageInfo.setCreateTime(new Date());
		messageInfo.setSender(sender);
		messageInfo.setStatus(MessageStatus.MSG_SEND_SUCCESS);
		save(messageInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MessageInfo> reciver(String to) throws DaoException {
		Session session = getSession();
		String queryStr = "from messageInfo where to = :to and status = :status";
		Query query = session.createQuery(queryStr);
		query.setParameter("to", to);
		query.setParameter("status", MessageStatus.MSG_SEND_SUCCESS);
		return query.list();
	}

	@Override
	public void reportSuccess(long msgId) throws DaoException {
		MessageInfo message = (MessageInfo) getSession().load(MessageInfo.class, msgId);
		message.setStatus(MessageStatus.MSG_RECIVER_OK);
		update(message);
	}

}
